/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RosaDosVentosFrequencia.java
 *
 * Created on 18/05/2010, 17:25:18
 */

package br.leiam.eolica.denis;

import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.chart.ChartUtilities;

import br.leiam.eolica.excecoes.EolicaException;

import sun.misc.BASE64Encoder;


/**
 * 
 * @author Denis
 */
public class RosaDosVentosFrequencia extends javax.swing.JPanel {
	private ResultSetTableModel tableModel;
	private String QUERY = "";
	private RosaDosVentosFrequenciaDirecao direcao;
	private String coluna = "direcao";
	private boolean haveData = false;

	/** Creates new form RosaDosVentosFrequencia 
	 * @throws Exception */
	public RosaDosVentosFrequencia(String JDBC_DRIVER, String DATABASE_URL,
			String USERNAME, String PASSWORD, String TABLE_NAME,
			String DataInicio, String DataFim, int altura, int tamx, int tamy) throws Exception {

		initComponents();
		this.setSize(tamx, tamx + 20);
		direcao = new RosaDosVentosFrequenciaDirecao();
		jPanel3.add(direcao);
		//coluna = dir;
		coluna = getColuna(altura);

		for (int x = 15; x < 345; x = x + 30) {
			String um = String.valueOf(x);
			String dois = String.valueOf(x + 30);
			String umm = String.valueOf(x * Math.PI / 180);
			String doiss = String.valueOf((x + 30) * Math.PI / 180);

			QUERY = QUERY + "select '" + um + "-" + dois
					+ "' as angulo, (select count(*) as ocorrencias from "
					+ TABLE_NAME + " where " + coluna + " > " + um + " and "
					+ coluna + " < " + dois
					+ " and datahora between timestamp('" + DataInicio
					+ "') and timestamp('" + DataFim
					+ "'))/(select count(*) from " + TABLE_NAME
					+ " where datahora between timestamp('" + DataInicio
					+ "') and timestamp('" + DataFim
					+ "'))*100 as frequencia union \n";
		}
		String um = String.valueOf(345);
		String dois = String.valueOf(375);
		String umm = String.valueOf(345 * Math.PI / 180);
		String doiss = String.valueOf(375 * Math.PI / 180);
		QUERY = QUERY + "select '" + um + "-" + dois
				+ "' as angulo, (select count(*) as ocorrencias from "
				+ TABLE_NAME + " where sin(3.1416*" + coluna + "/180) > sin("
				+ umm + ") and sin(3.1416*" + coluna + "/180) < sin(" + doiss
				+ ") and datahora between timestamp('" + DataInicio
				+ "') and timestamp('" + DataFim + "'))/(select count(*) from "
				+ TABLE_NAME + " where datahora between timestamp('"
				+ DataInicio + "') and timestamp('" + DataFim
				+ "'))*100 as frequencia";
		try {
			tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL,
					USERNAME, PASSWORD, QUERY);

			direcao.setTableModel(tableModel);
			if (tableModel.getRowCount() > 0)
				haveData = true;
		} catch (Exception ex) {
			Logger.getLogger(RosaDosVentosFrequencia.class.getName()).log(
					Level.SEVERE, null, ex);
		}

		direcao.repaint();
		Font fonte = jLabel5.getFont();
		float tamanhoFonte = this.getWidth() * 12 / 300;
		jLabel5.setFont(fonte.deriveFont(tamanhoFonte));
		jPanel1.setBounds(0, 0, this.getWidth(), this.getHeight());
		jPanel4.setBounds(0, 0, this.getWidth(), 20 + (int) tamanhoFonte);
		jLabel5.setBounds(0, 0, jPanel4.getWidth(), 15);
		jLabel1.setBounds(0, jPanel4.getHeight() - 15, jPanel4.getWidth(), 15);
		jLabel3.setBounds(0, this.getHeight() - 15, this.getWidth(), 15);
		jPanel5.setBounds(0, jPanel4.getHeight(), 15, this.getHeight()
				- jPanel4.getHeight() - jLabel3.getHeight());
		jLabel2.setBounds(0, 0, jPanel5.getWidth(), jPanel5.getHeight());
		jLabel4.setBounds(this.getWidth() - 15, jPanel4.getHeight(), 15,
				jPanel5.getHeight());
		jPanel3.setBounds(jPanel5.getWidth(), jPanel4.getHeight(),
				this.getWidth() - jPanel5.getWidth() - jLabel4.getWidth(),
				this.getHeight() - jPanel4.getHeight() - jLabel3.getHeight());
		direcao.setBounds(2, 2, jPanel3.getWidth() - 4, jPanel3.getHeight() - 4);

	}

	public String getBase64String() {
		String figura = "";

		this.revalidate();
		try {
			double escalaX = 1, escalaY = 1;
			Component co = this;
			BufferedImage bi = new BufferedImage(
					(int) (co.getSize().getWidth() * escalaX), (int) (co
							.getSize().getHeight() * escalaY),
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = bi.createGraphics();
			g2d.scale(escalaX, escalaY);
			co.paint(g2d);

			byte[] figBytes;

			figBytes = ChartUtilities.encodeAsPNG(bi);

			BASE64Encoder encoder = new BASE64Encoder();
			figura = encoder.encode(figBytes);
		} catch (Exception ex) {
			Logger.getLogger(Weibull.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return figura;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jPanel3 = new javax.swing.JPanel();
		jPanel4 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jPanel5 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();

		jPanel1.setBackground(new java.awt.Color(255, 255, 255));

		jPanel3.setBackground(new java.awt.Color(255, 255, 255));
		jPanel3.setLayout(new java.awt.BorderLayout());

		jPanel4.setBackground(new java.awt.Color(255, 255, 255));

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel1.setText("N");

		jLabel5.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
		jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel5.setText("FREQUENCIA DE OCORRENCIA POR DIRECAO");

		javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(
				jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout.setHorizontalGroup(jPanel4Layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jLabel1,
						javax.swing.GroupLayout.Alignment.TRAILING,
						javax.swing.GroupLayout.DEFAULT_SIZE, 407,
						Short.MAX_VALUE)
				.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
						407, Short.MAX_VALUE));
		jPanel4Layout
				.setVerticalGroup(jPanel4Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel4Layout
										.createSequentialGroup()
										.addComponent(jLabel5)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												21, Short.MAX_VALUE)
										.addComponent(jLabel1)));

		jPanel5.setBackground(new java.awt.Color(255, 255, 255));

		jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		jLabel2.setText("W");

		javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(
				jPanel5);
		jPanel5.setLayout(jPanel5Layout);
		jPanel5Layout.setHorizontalGroup(jPanel5Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 20,
				Short.MAX_VALUE));
		jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel5Layout
						.createSequentialGroup()
						.addComponent(jLabel2,
								javax.swing.GroupLayout.DEFAULT_SIZE, 211,
								Short.MAX_VALUE).addContainerGap()));

		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel3.setText("S");

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel4.setText("E");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(
												jPanel5,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jPanel3,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												357, Short.MAX_VALUE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jLabel4,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												20,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addComponent(jLabel3,
								javax.swing.GroupLayout.DEFAULT_SIZE, 407,
								Short.MAX_VALUE)
						.addComponent(jPanel4,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(
												jPanel4,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jPanel5,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jLabel4,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																222,
																Short.MAX_VALUE)
														.addComponent(
																jPanel3,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																222,
																Short.MAX_VALUE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel3)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		getAccessibleContext().setAccessibleParent(this);
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JPanel jPanel5;

	// End of variables declaration//GEN-END:variables

	/**
	 * @return the haveData
	 */
	public boolean isHaveData() {
		return haveData;
	}

	/**
	 * @param haveData
	 *            the haveData to set
	 */
	public void setHaveData(boolean haveData) {
		this.haveData = haveData;
	}
	
public static String getColuna(int altura) throws EolicaException {// FIXME Qual coluna retornar? Media, Max, Min ou desvio padrao??
		
		if (altura == 13) {
			return "r1a";
		} else if (altura == 100) {
			return "d1a";
		} else if (altura == 70) {
			return "d2a";
		} else  {
			throw new EolicaException("Nao existe medicao de direcao na altura de " + altura + " metros");
		} 
	}

}
